package com.ruoyi.project.business.tmpuserpost.service.impl;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.domain.model.daily.Post;
import com.ruoyi.project.business.tmpuserpost.entity.TmpUserpost;
import com.ruoyi.project.business.tmpuserpost.mapper.TmpUserpostMapper;
import com.ruoyi.project.business.tmpuserpost.request.AddTmpUserpostRequest;
import com.ruoyi.project.business.tmpuserpost.request.ListTmpUserpostRequest;
import com.ruoyi.project.business.tmpuserpost.response.TmpUserpostResponse;
import com.ruoyi.project.business.tmpuserpost.service.ITmpUserpostService;
import com.ruoyi.project.business.utils.BusinessUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 临工岗位关系Service业务层处理
 * 
 * @author ${author}
 * @date 2024-04-03
 */
@Service
public class TmpUserpostServiceImpl implements ITmpUserpostService {
    @Autowired
    private TmpUserpostMapper tmpUserpostMapper;

    /**
     * 查询临工岗位关系
     * 
     * @return 临工岗位关系
     */
    @Override
    public TmpUserpost selectTmpUserpostById(Long id) {
        return tmpUserpostMapper.selectTmpUserpostById(id);
    }

    /**
     * 查询临工岗位关系列表
     * 
     * @return 临工岗位关系
     */
    @Override
    public List<TmpUserpostResponse> selectTmpUserpostList(ListTmpUserpostRequest request, Long tenantId) {
        // TODO request to  param
        TmpUserpost param = BeanUtils.copy(request, TmpUserpost.class);
        param.setTenantId(tenantId);
        List<TmpUserpost> entitis = tmpUserpostMapper.selectTmpUserpostList(param);
        List<TmpUserpostResponse> resList = BeanUtils.copyList(entitis, TmpUserpostResponse.class);
        BusinessUtil.fillListNames(resList, tenantId);
        return resList;
    }

    /**
     * 新增临工岗位关系
     * 
     * @return 结果
     */
    @Override
    public int insertTmpUserpost(AddTmpUserpostRequest request, Long operatorId, Long tenantId) {
        TmpUserpost entity = BeanUtils.copy(request, TmpUserpost.class);
        entity.setTenantId(tenantId);
        return tmpUserpostMapper.insertTmpUserpost(entity);
    }

    /**
     * 修改临工岗位关系
     * 
     * @return 结果
     */
    @Override
    public int updateTmpUserpost(TmpUserpost entity, Long operatorId, Long tenantId) {
        return tmpUserpostMapper.updateTmpUserpost(entity);
    }

    /**
     * 批量删除临工岗位关系
     * 
     * @return 结果
     */
    @Override
    public int deleteTmpUserpostByIds(Long[] ids, Long operatorId, Long tenantId) {
        return tmpUserpostMapper.deleteTmpUserpostByIds(ids);
    }

    /**
     * 删除临工岗位关系信息
     * 
     * @return 结果
     */
    @Override
    public int deleteTmpUserpostById(Long id, Long operatorId, Long tenantId) {
        return tmpUserpostMapper.deleteTmpUserpostById(id);
    }

    @Override
    public Map<Long, List<Post>> selectTmpUserpostByUserIds(List<Long> userIds, Long tenantId) {
        List<TmpUserpost> tmpUserposts = tmpUserpostMapper.selectTmpUserpostByUserIds(userIds, tenantId);
        if(CollectionUtils.isNotEmpty(tmpUserposts)) {
            Map<Long, List<Post>> collect = tmpUserposts.stream().map(tmp -> {
                Post post = new Post();
                post.setId(tmp.getTmpPostId());
                post.setUserId(tmp.getUserId());
                post.setName(tmp.getTmpPostName());
                return post;
            }).collect(Collectors.groupingBy(Post::getUserId));
            return collect;
        }
        return new HashMap<>();
    }
}
